<?php

class Schedule_list extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Schedule_list':
	 * @var integer $id
	 * @var integer $fk_consignor_id
	 * @var integer $fk_path_id
	 * @var string $distributor
	 * @var string $deliver_time
	 * @var string $arrival_time
	 * @var string $goods_name
	 * @var integer $fk_goods_name_id
	 * @var integer $unit_name
	 * @var double $unit_price
	 * @var integer $amount
	 * @var double $mileage
	 * @var double $freight_charge
	 * @var string $operator
	 * @var string $region
	 * @var integer $state
	 * @var string $create_time
	 */
	
	public $consignor_name;
	public $deliver_place;
	public $arrival_place;
	public $goods_name;
	public $user_name;
	public $user_region;
	public $mileage;
	//private $_unit_name=array('0'=>'车 ','1'=>'托', '2'=>'吨 ','3'=>'方', '4'=>'吨公里', '5'=>'其他');
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'schedule_list';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('distributor','length','max'=>30),
			array('fk_consignor_id, fk_path_id, fk_goods_name_id, unit_name', 'numerical', 'integerOnly'=>true),
			array('unit_price, mileage, freight_charge,amount', 'numerical'),
			array('consignor_name,distributor,deliver_place,arrival_place,deliver_time,arrival_time,
			unit_price,unit_name,amount,mileage,freight_charge,user_name,user_region,goods_name', 'required'),
			array('amount', 'match','pattern' => '/^\d{1,8}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多8位，小数位2位'),
			array('unit_price,freight_charge', 'match','pattern' => '/^\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('deliver_time,arrival_time', 'match','pattern' => '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/','message'=>'{attribute} 必须为日期型(年-月-日  时:分)'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		return true;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
	
	public function getUnitNames(){
		return Yii::app()->params['unit_name'];
	}
	
	public function getUnitName($n=null){
		return Yii::app()->params['unit_name'][$n==null?$this->unit_name:$n];
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'fk_consignor_id' => '货主(发货人)id',
			'consignor_name' => '货主发货人',
			'fk_path_id' => '线路id',
			'deliver_place' => '起运地',
			'arrival_place' => '到达地',
			'distributor' => '下发计划人',
			'deliver_time' => '装车时间',
			'arrival_time' => '要求到达时间',
			'goods_name' => '货物名称',
			'unit_name' => '计算标准',
			'unit_price' => '单价(元)',
			'amount' => '货物数量',
			'mileage' => '里程(公里)',
			'freight_charge' => '运费(元)',
			'fk_user_id' => '录入员id',
			'user_name' => '录入人',
			'user_region' => '所属区域',
			'state' => '状态',
			'create_time' => '创建时间',
		);
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='consignor_name';
		$params[]='deliver_place';
		$params[]='arrival_place';
		$params[]='goods_name';
		$params[]='user_name';
		$params[]='user_region';
		$params[]='mileage';
		return $params;
	}
}